package com.paas.form.queue;

import com.paas.plus.tool.form.model.ApiLog;
import lombok.extern.slf4j.Slf4j;

import java.util.ArrayList;
import java.util.List;
import java.util.Queue;
import java.util.concurrent.ConcurrentLinkedQueue;
import java.util.concurrent.locks.ReentrantLock;

@Slf4j
public class FormApilogServer extends Thread{

    private ReentrantLock lock=new ReentrantLock();
    private static volatile FormApilogServer formApiServer;

    private static Queue<ApiLog> apiRequestQueue=null;

    private FormApilogConsumer formApiConsumer;

    private final long spliteTime=1000 *10;
    /**
     * 单例模式
     * @return
     */
    public static FormApilogServer getInstance() {
        if (formApiServer == null) {
            synchronized (FormApilogServer.class) {
                if (formApiServer == null) {
                    formApiServer = new FormApilogServer();
                    formApiServer.setName("Thread-Oper-Log");
                }
            }
        }
        return formApiServer;
    }

    /**
     * 描述:创建日志服务器
     */
    public FormApilogServer() {
        apiRequestQueue=new ConcurrentLinkedQueue<ApiLog>();
    }
    /**
     * 描述
     * @param apiLog
     */
    public static synchronized void offer(ApiLog apiLog){
        if(apiRequestQueue!=null && apiLog!=null){
            apiRequestQueue.offer(apiLog);
        }
    }
    /**
     * 描述:获取数组信息
     * @return
     * @throws InterruptedException
     */
    public static synchronized ApiLog poll() {
        return apiRequestQueue.poll();
    }

    /**
     * 描述:获取队列当前的大小
     * @return
     */
    public static int getSize(){
        return apiRequestQueue.size();
    }

    @Override
    public void run(){
        while (true){
            try{
                Thread.sleep(1000 *10);
            }catch(InterruptedException e){
                //e.printStackTrace();
                log.error(e.getMessage());
            }
            try {
                int size = apiRequestQueue.size();
                int count=0;
                if (size > 0) {
                    log.debug("當前操作日誌队列数"+size);
                    List<ApiLog> list = null;
                    for (int i = 0; i < size; i++) {
                        if (list == null) {
                            list = new ArrayList<ApiLog>();
                        }
                        list.add(poll());
                        count++;
                        if(count%100==0){
                            if (list != null) {
                                formApiConsumer.save(list);
                            }
                            list = null;
                        }
                    }
                    if (list != null) {
                        formApiConsumer.save(list);
                    }
                }
            } catch (Exception e) {
                // e.printStackTrace();
                log.error(e.getMessage());
            }
        }
    }

    public FormApilogConsumer getFormApiConsumer() {
        return formApiConsumer;
    }

    public void setFormApiConsumer(FormApilogConsumer formApiConsumer) {
        this.formApiConsumer = formApiConsumer;
    }
}
